import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 083. 没有重复元素集合的全排列
 * @author: WXY
 * @create: 2023-01-20 20:04
 * @Version 1.0
 **/
public class offer_twentyeight2_permute {
    public static List<List<Integer>> permute(int[] arr) {
        if (arr == null || arr.length == 0) {
            return new ArrayList<>();
        }
        List<List<Integer>> all = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        boolean[] used = new boolean[arr.length];
        process(arr, all, path,used);
        return all;
    }

    private static void process(int[] arr, List<List<Integer>> all, List<Integer> path, boolean[] used) {
        if (path.size() == arr.length) {
            all.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            if (used[i] == true)continue;
            path.add(arr[i]);
            used[i] = true;
            process(arr, all, path, used);
            used[i] = false;
            path.remove(path.size() - 1);
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        List<List<Integer>> permute = permute(arr);
        System.out.println(permute);
    }
}
